import Sequelize from 'sequelize'
import db from '../database'

const StudentInfo = db.define('student_info',{
	id:{
		type: Sequelize.INTEGER,
        autoIncrement:true,
        allowNull: false
	},
    sno:{
    	type:Sequelize.STRING,
    	primaryKey:true,
        allowNull: false
    },
    sname:{
        type:Sequelize.STRING,
    },
    password:{
    	type:Sequelize.STRING,
    },
    sex:{
        type:Sequelize.STRING,
    },
    classname:{
        type:Sequelize.STRING,
        foreignKey:true
    },
    phone:{
        type:Sequelize.STRING,
    },
    birth:{
        type:Sequelize.STRING,
    },
    address:{
        type:Sequelize.STRING,
    },
    role:{
        type:Sequelize.STRING,
    },
},{
	freezeTableName:false,
	tableName:'student_info',
    updatedAt:'update_time',
    createdAt:'create_time'
});

class StudentModel {
	add(record){
		return StudentInfo.create(record);
	}

	update(record){
		return StudentInfo.update(record,{
			where:{
				sno:record.sno
			}
		})
	}

	delete(sno){
		return StudentInfo.destroy({
			where:{
				sno:sno
			}
		})
	}

	findById(sno){
		return StudentInfo.findById(sno);
	}

	findByPage(record){
		return StudentInfo.findAndCount({
			limit: record.page_size,
			offset: record.page_size * (record.page_no - 1)
		})
	}

	findAllData(param){
		return StudentInfo.findAll({
            where:param
        });
	}

    findByName(record){
        return db.query(`SELECT * FROM student_info WHERE sname LIKE '${record}%'`);
    }

    findByNo(record){
        return db.query(`SELECT * FROM student_info WHERE sno LIKE '${record}%'`);
    }

    updatePassword(record){
        return db.query(`UPDATE student_info SET password = ${record.password} WHERE sno LIKE '${record.sno}%'`);
    }
}

module.exports = new StudentModel();